package com.hello;

import java.util.ArrayList;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;

public class SendEmergencySms {
	private Context context;
	
	public SendEmergencySms(Context cont){
		this.context = cont;
	}
	
	 // sends an SMS message to another device
    void sendSMS(String phoneNumber, String message)
    {
    	SmsManager sms = SmsManager.getDefault();
    	ArrayList<String> msgs = sms.divideMessage(message);
    	if (msgs.size() == 1 ){
    		final PendingIntent pi = PendingIntent.getActivity(context, 0,
    	   			 new Intent(context, SendEmergencySms.class), 0);
    	        sms.sendTextMessage(phoneNumber, null, message, pi, null);
    	}
    	else{
    		 ArrayList<PendingIntent> listOfIntents = new ArrayList<PendingIntent>();  
    		  
             for (int i=0; i < msgs.size(); i++){  
                 Intent sentIntent = new Intent();  
                 PendingIntent pi = PendingIntent.getBroadcast(context, 0, sentIntent, PendingIntent.FLAG_CANCEL_CURRENT);  
                 listOfIntents.add(pi);  
             }  
             sms.sendMultipartTextMessage(phoneNumber, null, msgs, listOfIntents,null);
    	}
    	        
    } 
	
}
